package com.base.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lazyzhao.util.pagination.Pagination;

public class ModuleService extends BasicService {
	public ModuleService() {
		TABLENAME_MODULE_VIEW = "(select d.* from Module d) Z";
	}

	public List findModules(Connection conn, Map params, int startend)
			throws SQLException {
		int start = 0;
		int end = 0x3b9ac9ff;
		if (startend > 1)
			start = startend;
		return queryMapList(conn, tname("Module"), params, start, end);
	}

	public List<Map<String, Object>> findModulesBySql(Connection conn,
			String username) throws SQLException {
		String sql = "select m.* from user_module_purview ump ,module m,users u where ump.moduleid = m.id and ump.userid = u.id and u.username = '"
				+ username + "'";
		List<Map<String, Object>> list = queryMapListBySql(conn, sql, null);
		return list;
	}

	public Pagination findModulesView(Connection conn, Map params,
			Pagination pagination) throws SQLException {
		return query(conn, TABLENAME_MODULE_VIEW, params, pagination);
	}

	public int insertModules(Connection conn, Map objects) throws SQLException {
		return query.insert(conn, tname("Module"), objects);
	}

	public int updateModule(Connection conn, Map<String, Object> objects,
			String... wheres) //transient wheres[]
			throws SQLException {
		return query.update(conn, tname("Module"), objects, wheres);
	}

	public int updateModules(Connection conn, List objects, String... wheres) //transient
			throws SQLException {
		return query.update(conn, tname("Module"), objects, wheres);
	}

	public int deleteModules(Connection conn, String[] ids) throws SQLException {
		Object[][] objs = new Object[ids.length][];
		for (int i = 0; i < ids.length; i++) {
			objs[i] = new Object[] { ids[i] };
		}
		int[] ints = query.batch(conn, "delete from "
				+ tname(TABLENAME_SERVICES) + " where id=?", objs);
		int count = 0;
		for (int i : ints) {
			count += i;
		}
		return count;
	}

	public static final String TABLENAME_SERVICES = "Module";
	public static final String TABLENAME_APPS = "apps";
	public String TABLENAME_MODULE_VIEW;
}
